package com.xk._02算法篇._01sort.sort.cmp;

import com.xk._02算法篇._01sort.sort.Sort;
import com.xk.tools.Integers;

/**
 * @description: 选择排序：最小的元素放在最左边
 * @author: xu
 * @date: 2022/10/1 4:34
 */
public class SelectionSort2<E extends Comparable<E>> extends Sort<E> {
    /**
     * 选择排序：最小的元素放在最左边
     */
    private void selectionSort2() {
        for (int left = 0; left < array.length - 1; left++) {
            int minIndex = left;
            for (int begin = left + 1; begin < array.length; begin++) {
                if (cmp(minIndex, begin) > 0) {
                    minIndex = begin;
                }
            }
            swap(minIndex, left);
        }
    }

    @Override
    protected void sort() {
        selectionSort2();
    }

    public static void main(String[] args) {
        Integer[] array1 = Integers.random(20, 1, 100);
        Integers.println(array1);

        Sort<Integer> selectionSort = new SelectionSort2<>();
        selectionSort.sort(array1);
        System.out.println(selectionSort);
        Integers.println(array1);
        System.out.println("Integers.isAscOrder(array1) = " + Integers.isAscOrder(array1));
    }
}
